package com.esir.android.base;

import android.os.Bundle;

import androidx.annotation.Nullable;

import com.esir.android.base.mvp.IBasePresenter;
import com.esir.android.base.mvp.IBaseView;
import com.esir.android.util.T;

/**
 * @author 黄嵘才(HuangRc)
 */
public abstract class BaseMvpActivity<P extends IBasePresenter> extends BaseActivity
        implements IBaseView {

    private P mPresenter;
    private ContextX contextX = null;
    /**
     * 获取注入的Presenter
     *
     * @return Presenter
     */
    protected P getPresenter() {
        return mPresenter;
    }

    /**
     * 请在Presenter层接受事件
     * 为了使MVP层V层的处理更加集加于View.Event不应再注册了
     * <p>如果你仍需要使用，建议使用BaseActivity
     *
     * @return false 不再注册事件;
     */
    @Override
    protected final boolean regEvent() {
        return false;
    }

    @Override
    protected void initView() {

    }

    @Override
    public ContextX getContextX() {
        if (contextX == null) {
            contextX = new ContentXWrapper(this);
        }
        return contextX;
    }

    @Override
    public void showMsg(Object tag, String msg) {
        T.showToast(this, msg);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //创建present
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView(this);
        }
    }

    /**
     * 创建Presenter
     */
    protected abstract P createPresenter();
}
